// components/comment/comment.js
import { _memberLike, _likeCollectOperate, _memberCollect} from '../../utils/qiaofinn.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    modelData:Object,
    showComment:Boolean
  },

  /**
   * 组件的初始数据
   */
  data: {
    disable_input:false,
    jianpanHeight:0,
    comment:'',
    emojiSource: 'https://res.wx.qq.com/op_res/eROMsLpnNC10dC40vzF8qviz63ic7ATlbGg20lr5pYykOwHRbLZFUhgg23RtVorX',
  },

  /**
   * 组件的方法列表
   */
  methods: {

    // 点赞
  likeTap(){
    _memberLike(this.data.modelData.id,this.data.modelData.model_name,res=>{
      if(res.statusCode===200 && res.data.status == true){
        const new_model_data = _likeCollectOperate('like',this.data.modelData)
        this.setData({
          modelData:new_model_data
        })
      }
    })
  },
  // 收藏
  collecTap(){
    _memberCollect(this.data.modelData.id,this.data.modelData.model_name,res=>{
      console.log(res)
      if(res.statusCode===200 && res.data.status == true){
        const new_model_data = _likeCollectOperate('collect',this.data.modelData)
        this.setData({
          modelData:new_model_data
        })
      }else{
        wx.showToast({
          icon:'none',
          title: '请求失败，请稍后重试!',
        })
      }
    })
  },

    //发送
  sendComment(){
    // console.log('sendComment')
    // console.log(this.data)
    // const {comment,contentType,instanceId} = this.data
    // _sendComment(comment,contentType,instanceId,res=>{
    //   console.log(res)
    // })
    // //解析评论
    // // //用户对评论的解析
    // const emojiInstance = this.selectComponent('.mp-emoji')
    // this.parseEmoji = emojiInstance.parseEmoji;
    // const emoji=this.parseEmoji(this.data.comment)
    // console.log(emoji)
    // this.setData({
    //   parsedComment:emoji,
    //   comment:'',
    //   disable_input:false,
    //   showEmoji:true,
    //   testareagetFocus:false,
    // })
    
  },
      // testarea获得焦点时，获取键盘的高度
  textareaFocus:function(res){
    console.log('获取键盘高度')
    console.log(res.detail.height)
    this.setData({
      showEmoji:true,
      jianpanHeight:230,
    })
  },
  //开始评论
  startCommentTap:function(){

    this.triggerEvent('startCommentTap');
    // this.setData({
    //   disable_input:true,
    //   testareagetFocus:true
    // })
  },
  // 监听键盘高度变化
  onkeyboardHeightChange(e){
    const {height} = e.detail
    if(height == 0){
      this.setData({
        jianpanHeight:0
      })
    }
},
  //点击表情
  emojiTab(){
    this.setData({
      showEmoji:false,
      jianpanHeight:0,
      testareagetFocus:false
    })
  },
  //点击键盘
  jianpanTap(){
    this.setData({
      testareagetFocus:true
    })
  },
  //键盘输入时
  textareaInput:function(res){
    this.data.cursor = res.detail.cursor || 0
    this.setData({
      comment:res.detail.value
    })
  },
    // 插入表情
  insertEmoji(evt) {
    const emotionName = evt.detail.emotionName
    const {cursor,comment} = this.data
    const newComment = comment.slice(0, cursor) + emotionName + comment.slice(cursor)
    this.setData({
      comment: newComment,
      cursor: cursor + emotionName.length
    })
  },
  //遮罩
  maskTap:function(){
    this.setData({
      disable_input:false,
      showEmoji:true,
      testareagetFocus:false
    })
  },

  }
})

